package com.fms.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fms.common.utils.JwtUtils;
import com.fms.common.vo.Result;
import com.fms.sys.entity.Card;
import com.fms.sys.entity.Goal;
import com.fms.sys.entity.User;
import com.fms.sys.service.IGoalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jinfeng
 * @since 2023-03-18
 */
@RestController
@RequestMapping("/goal")
public class GoalController {

    @Autowired
    private IGoalService goalService;

    @GetMapping("/list")
    public Result<Map<String, Object>> getGoalList(
            @RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize) {

        // 获取当前用户ID
        Long userId = JwtUtils.getCurrentUserId();

        LambdaQueryWrapper<Goal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goal::getUserId, userId);
        wrapper.orderByDesc(Goal::getId);

        Page<Goal> page = new Page<>(pageNo, pageSize);
        goalService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    @PostMapping
    public Result<?> addGoal(@RequestBody Goal goal) {
        // 验证标题是否为空
        if (goal.getTitle() == null || goal.getTitle().trim().isEmpty()) {
            return Result.fail("标题不能为空");
        }
        // 设置当前用户ID
        goal.setUserId(JwtUtils.getCurrentUserId());
        // 设置初始已存金额为0
        goal.setSavedAmount(goal.getSavedAmount() != null ? goal.getSavedAmount() : new java.math.BigDecimal("0"));
        goalService.save(goal);
        return Result.success("新增心愿成功");
    }

    @PutMapping
    public Result<?> updateGoal(@RequestBody Goal goal) {
        // 验证是否是当前用户的心愿
        Goal existingGoal = goalService.getById(goal.getId());
        if (existingGoal == null || !existingGoal.getUserId().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("无权修改此心愿");
        }
        goalService.updateById(goal);
        return Result.success("修改心愿成功");
    }

    @GetMapping("/{id}")
    public Result<Goal> getGoalById(@PathVariable("id") Long id) {
        Goal goal = goalService.getById(id);
        if (goal == null || !goal.getUserId().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("心愿不存在或无权查看");
        }
        return Result.success(goal);
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteGoalById(@PathVariable("id") Long id) {
        Goal goal = goalService.getById(id);
        if (goal == null || !goal.getUserId().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("心愿不存在或无权删除");
        }
        goalService.removeById(id);
        return Result.success("删除心愿成功");
    }

}
